import { IFeidaoAiNodejsComponent } from '@feidao/web/interfaces';

import render from '@feidao/web/atom/nodejs/render/render';
import nodejs from '@feidao/web/atom/nodejs/msg-b/nodejs';

import server_render from '../atom-n/server-render';
import { Params } from '../atom/params2query';
import p2q from '../atom/params2query';
import generate_query4render from '../atom/generate-query4render';
import generate_sort_fields from '../atom/generate-sort-fields';

import P01 from './P01';
import P02 from './P02';
import P04 from './P04';
import P03 from './P03';
import P05 from './P05';

//  "_id":"id","no":"编号","resume_name":"候选者姓名","sex":"候选者性别","age":"候选者年龄","docking_date":"应聘日期","post_id":"应聘岗位","exec_plan_no":"招聘计划编号","work_plan":"招聘计划名称","score":"面试得分","written_score":"笔试得分","resume_interview_comment":"面试评价","reexamine_com":"复试评价","staus":"审核状态","audit_opinion":"审核意见","auditname":"审核人","auditdate":"审核日期","result":"应聘结果","accessory_url":"附件,"

const name2title = {
	'resume_name': '候选者姓名',
	'sex': '候选者性别',
	'age': '候选者年龄',
	'post_id': '应聘岗位',
	'docking_date': '应聘日期',
	'work_plan': '招聘计划名称',
	'score': '面试得分',
	'written_score': '笔试得分',
	'result': '应聘结果',
	'auditname': '审核人',
	'auditdate': '审核日期',
	'staus': '审核状态'
};

function create_data_sort(fd: IFeidaoAiNodejsComponent) {
	try {
		const fields = ['resume_name', 'sex', 'age', 'post_id', 'docking_date', 'work_plan', 'score', 'written_score', 'result', 'auditname', 'auditdate', 'staus'];
		const params = fd.data.params;
		delete params['page-no'];
		const data = generate_sort_fields(fd.data.url, fd.data.params as Params, fields);
		data.forEach((d) => {
			const field = d.field;
			render(fd.data.node, d, P02, field);
		});
	} catch (err) {
		console.error('------err', err);
	}
}

export default async function nba001(fd: IFeidaoAiNodejsComponent, ...args: any[]) {
	let page_no = fd.data.params['page-no'];
	if (!page_no) {
		page_no = 1;
	}
	let page_size = fd.data.params['page_size'];
	if (!page_size) {
		page_size = 20;
	}
	const q = p2q(fd.data.params as Params);
	const query = {
		filter: q.criteria,
		sort: q.sort,
		page_no,
		page_size
	};
	const data = generate_query4render(fd.data.url, fd.data.params as Params, name2title);
	render(fd.data.node, data, P01, 'P01');

	// 排序
	create_data_sort(fd);
	// 没有数据
	const res = await nodejs(fd.data.spaceid, "interview_info_find", query);
	// 无数据渲染P04
	if (res.dataArray.length == 0) {
		server_render(fd.data.node, 'P04', P04, '');
	} else {
		server_render(fd.data.node, 'P03', P03, res.dataArray);
	}

	res['page_size'] = page_size;
	render(fd.data.node, res, P05, 'P05');
}